package com.ruoyi.common.utils.ofd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.ruoyi.common.utils.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class FxConvertUtils {

//      网关地址
    static String conUrlGateWay = "http://123.207.220.137:8888/gsdk-service";
//    普通地址
    static String conUrl = "http://123.207.220.137:8888";
//   上传文件
    static String upload = "/api/file/upload";
//  获取token
    static String token = "/api/auth/token";
//  带token转版
    static String conToken = "/api/auth/convert/submit";
//   不带token转版
    static String conNoToken = "/api/convert/submit";
//获取token的信息  在转版页面上 应用注册获取
    static String clientId = "uploi3o3x3v9epygrcr314el";
    static String clientSecret = "lqtaxudkxsqs5mbyvla0tv1stqp7dt0a";
    static String apiServerName = "cltess";

//    转版回传接口
    static String fxcallback = "http://192.168.200.3:8092/hwcollection/fxconvert/callback";

    /**
     * 上传文件
     *
     * @return
     */
    public static String uploadFile(InputStream in,String name) {
        // 创建httpPost
        HttpPost httpPost = new HttpPost(conUrlGateWay + upload);
        Map<String, String> header = new HashMap<String, String>();
        try {
            // 设置参数
            MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
            multipartEntityBuilder.addBinaryBody("file", in , ContentType.DEFAULT_BINARY,name);
            HttpEntity reqEntity = multipartEntityBuilder.build();
            httpPost.setEntity(reqEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        String responseContent = null;
        try {
            // 创建默认的httpClient实例.
            httpClient = HttpClients.createDefault();
            // 执行请求
            response = httpClient.execute(httpPost);
            entity = response.getEntity();
            responseContent = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭连接,释放资源
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return responseContent;
    }


    /**
     * @return
     */
    public static String convertFileNoToken(UploadFileFX fileOb,int newFormat) {

        String convertUrl = conUrlGateWay + conNoToken;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("taskId", EssUuIdUtil.getUuId().replace("-", ""));

//        异步回传
//        jsonObject.put("callbackUrl",fxcallback);
//        jsonObject.put("isSync", false);

//         同步
        jsonObject.put("isSync", true);

        JSONArray objects = new JSONArray();
        JSONObject jsonFileCon = new JSONObject();
        jsonFileCon.put("bucket", fileOb.getBucket());
        jsonFileCon.put("fileId", EssUuIdUtil.getUuId().replace("-", ""));
        jsonFileCon.put("path", fileOb.getPath());
        jsonFileCon.put("fileName", fileOb.getFileName());
        jsonFileCon.put("format", fileOb.getFormat());
//        输出格式 1、OFD 2、PDF 3、PDF/A  4、TXT 5、DOC 6、JPG 7、PNG 8、BMP 9、JSON
        jsonFileCon.put("newFormat", newFormat);

        objects.add(jsonFileCon);
        jsonObject.put("list", objects);
        HashMap<String, Object> map = new HashMap<>();
        map.put("req", jsonObject);
        String post = HttpUtils.HttpPostWithJson(convertUrl, jsonObject.toString());
        return post;

    }

    /**
     * 获取认证token
     *
     * @return
     */
    public static String authToken() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("clientId", clientId);
        jsonObject.put("clientSecret", clientSecret);
        jsonObject.put("apiServerName", apiServerName);

        String post = HttpUtils.HttpPostWithJson(conUrlGateWay + token, jsonObject.toString());

        if (StringUtils.isNotEmpty(post)) {
            JSONObject jsonToken = JSONObject.parseObject(post);
            JSONObject data1 = jsonToken.getJSONObject("data");
            return (String) data1.get("token");
        }

        return "";
    }
    /**
     * token转版
     * @return
     */
    public static String convertFileToken(UploadFileFX fileOb, String token) {

        String convertUrl = conUrlGateWay + conToken;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("taskId", EssUuIdUtil.getUuId().replace("-", ""));
        jsonObject.put("token", token);
        jsonObject.put("isSync", true);
        JSONArray objects = new JSONArray();
        JSONObject jsonFileCon = new JSONObject();
        jsonFileCon.put("bucket", fileOb.getBucket());
        jsonFileCon.put("fileId", EssUuIdUtil.getUuId().replace("-", ""));
        jsonFileCon.put("path", fileOb.getPath());
        jsonFileCon.put("fileName", fileOb.getFileName());
        jsonFileCon.put("format", fileOb.getFormat());
//        输出格式 1、OFD 2、PDF 3、PDF/A  4、TXT 5、DOC 6、JPG 7、PNG 8、BMP 9、JSON
//        jsonFileCon.put("newFormat", 1);
        jsonFileCon.put("newFormat", 2 );
        objects.add(jsonFileCon);
        jsonObject.put("list", objects);
        HashMap<String, Object> map = new HashMap<>();
        map.put("req", jsonObject);
        String post = HttpUtils.HttpPostWithJson(convertUrl, jsonObject.toString());
        return post;
    }
}
